package middleware

import (
	"go-api/pkg/logger"
	"time"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

func ZapLogger() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		start := time.Now()
		// 处理请求
		ctx.Next()
		duration := time.Since(start)
		// 使用 Zap 记录日志
		logger.Info("HTTP request",
			zap.String("method", ctx.Request.Method),
			zap.String("path", ctx.Request.URL.Path),
			zap.String("query", ctx.Request.URL.RawQuery),
			zap.String("ip", ctx.RemoteIP()),
			zap.String("user_agent", ctx.Request.UserAgent()),
			// zap.Int("status", ctx.Request.Response.StatusCode),
			zap.Duration("duration", duration),
			zap.String("duration_human", duration.String()))
	}
}
